# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('heimdal' 'heimdal-libs' 'heimdal-devel')
pkgver=1.5.3
pkgrel=6
pkgdesc="Implementation of Kerberos V5 libraries"
arch=('i686' 'x86_64')
url="http://www.h5l.org/"
license=('custom')
makedepends=('libdb-devel' 'libcrypt-devel' 'libsqlite-devel' 'libreadline-devel' 'openssl-devel') #libldap-devel
options=('!libtool' '!emptydirs' 'staticlibs')
groups=('net-utils')
source=(http://www.h5l.org/dist/src/heimdal-${pkgver}.tar.gz
        1.2.1-test-modules.patch
        1.5.2-hdbdir.patch
        1.5.2-install-catman.patch
        1.5.2-roken-signal.patch
        heimdal-1.5.2-msys2.patch
        )
md5sums=('30b379e3de12f332fbd201131f02ffca'
         '11fc922e2d294570b00290466a4922da'
         '1a7f12d2d147e0f0e75c8f74ca44ee9d'
         '6ad64b8dfc6360a5e2b9d84b8a86abf1'
         'f07e91c3df57d9fb03356dff98f45b82'
         '4b3694cb496ca6f9b2bac802614c0b02')

prepare() {
  cd ${srcdir}/heimdal-${pkgver}

  patch -p2 -i ${srcdir}/1.2.1-test-modules.patch
  patch -p2 -i ${srcdir}/1.5.2-hdbdir.patch
  patch -p2 -i ${srcdir}/1.5.2-install-catman.patch
  patch -p2 -i ${srcdir}/1.5.2-roken-signal.patch
  patch -p1 -i ${srcdir}/heimdal-1.5.2-msys2.patch

  autoreconf -fi
}

build() {
  cd ${srcdir}/heimdal-${pkgver}

  ./configure --build=${CHOST} \
    --prefix=/usr \
    --libexecdir=/usr/libexec \
    --sbindir=/usr/bin \
    --sysconfdir=/etc/krb5 \
    --mandir=/usr/share/man \
    --datadir=/var/lib/heimdal \
    --localstatedir=/var/lib/heimdal \
    --with-openssl=/usr \
    --with-readline=/usr \
    --with-sqlite3=/usr \
    --enable-shared=yes \
    --enable-static \
    --without-x \
    --disable-kcm \
    --disable-ndbm-db \
    --disable-heimdal-documentation
  
  make LDFLAGS="${LDFLAGS} -no-undefined"
  setup
}

setup() {
  local destdir=${srcdir}/dest
  cd ${srcdir}/heimdal-${pkgver}
  make DESTDIR=${destdir} install

  # Remove daemons and their manpages
  for i in telnetd ftpd rshd; do
    rm ${destdir}/usr/share/man/man8/${i}.8
    rm ${destdir}/usr/libexec/${i}
  done

  # Rename clients and their manpages
  for i in rcp rsh telnet ftp su login; do
    if [ -f ${destdir}/usr/share/man/man1/${i}.1 ]; then
      mv ${destdir}/usr/share/man/man1/{,k}${i}.1
    fi
    mv ${destdir}/usr/bin/{,k}${i}
  done
  rm -rf ${destdir}/usr/share/man/cat{1,3,5,8}

  # install config
  # Do not install the config as this will conflict with krb5
  install -D -m644 ${srcdir}/heimdal-${pkgver}/krb5.conf ${destdir}/etc/krb5/krb5.conf

  # Remove conflicts
  rm -rf ${destdir}/usr/share/man/man5/ftpusers.5*                     # man-pages
  rm -rf ${destdir}/usr/share/man/man3/{DES,DH,EVP,OpenSSL,RAND,RSA}*  # openssl
  rm -rf ${destdir}/usr/share/man/man3/os.3*                           # erlang

  # Install the license
  install -Dm644 ${srcdir}/heimdal-${pkgver}/LICENSE \
    ${destdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_heimdal() {
  groups=('sys-utils')
  depends=('heimdal-libs')
  backup=(etc/krb5/krb5.conf)

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  rm -f ${pkgdir}/usr/bin/*.dll
  cp -rf ${srcdir}/dest/etc ${pkgdir}/
  cp -rf ${srcdir}/dest/usr/libexec ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
}

package_heimdal-libs() {
  depends=('libdb' 'libcrypt' 'libsqlite' 'libopenssl' 'libreadline')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_heimdal-devel() {
  pkgdesc="Heimdal headers and libraries"
  groups=('development')
  depends=('heimdal-libs' 'libcrypt-devel' 'libdb-devel' 'libsqlite-devel' 'libreadline-devel')

  mkdir -p ${pkgdir}/usr

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

  mkdir -p ${pkgdir}/usr/bin
  cp ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
}
